package com.example.design.ddd.practice.application.orgmng;

import com.example.design.ddd.practice.common.framework.exception.BusinessException;
import com.example.design.ddd.practice.domain.orgmng.entity.Org;
import com.example.design.ddd.practice.domain.orgmng.factory.OrgBuilder;
import com.example.design.ddd.practice.domain.orgmng.factory.OrgBuilderFactory;
import com.example.design.ddd.practice.domain.orgmng.orgservice.OrgHandler;
import com.example.design.ddd.practice.domain.orgmng.repository.OrgRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @Description
 * @Author liuzhihui
 * @Date 2024/5/31
 */
@Service
public class OrgService {
    @Autowired
    OrgRepository orgRepository;

    @Autowired
    OrgBuilderFactory orgBuilderFactory;

    @Autowired
    OrgHandler orgHandler;

    public OrgResponse addOrg(CreateOrgRequest request, Long userId) throws Throwable {
        OrgBuilder orgBuilder = orgBuilderFactory.create();
        Org org = orgBuilder.tenantId(request.getTenantId())
                .orgTypeCode(request.getOrgTypeCode())
                .leaderId(request.getLeaderId())
                .superiorId(request.getSuperiorId())
                .name(request.getName())
                .createdBy(userId).build();
        org = orgRepository.save(org);
        return buildOrgDto(org);
    }

    @Transactional
    public OrgResponse updateOrgBasic(Long id, UpdateOrgBasicRequest request, Long userId) {
//        Org org = orgRepository.findById(request.getTenantId(), id).orElseThrow(() -> {
//            throw new BusinessException("要修改的组织(id =" + id + " )不存在！");
//        });
//        orgHandler.updateBasic(org, request.getName(), request.getLeaderId(), userId);
//        orgRepository.update(org);
//        return buildOrgDto(org);
        return null;
    }

    //取消组织
    @Transactional
    public Long cancelOrg(Long id, Long tenant, Long userId) {
//        Org org = orgRepository.findById(tenant, id).orElseThrow(() -> {
//            throw new BusinessException("要取消的组织(id =" + id + " )不存在！");
//        });
//        orgHandler.cancel(org, userId);
//        orgRepository.update(org);
//        return org.getId();
        return null;
    }


    private static OrgResponse buildOrgDto(Org org) {
        OrgResponse response = new OrgResponse();
        response.setId(org.getId());
        response.setTenantId(org.getTenantId());
        response.setOrgTypeCode(org.getOrgTypeCode());
        response.setName(org.getName());
        response.setLeaderId(org.getLeaderId());
        response.setSuperiorId(org.getSuperiorId());
        response.setStatusCode(org.getStatus().getKey());
        response.setCreatedBy(org.getCreatedBy());
        response.setCreatedAt(org.getCreatedAt());
        response.setLastUpdatedBy(org.getLastUpdatedBy());
        response.setLastUpdatedAt(org.getLastUpdatedAt());
        return response;
    }
}
